Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Access] Add unit test for websocket controller #6762

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

illia-malachyn
Copy link
Contributor

@illia-malachyn illia-malachyn commented Nov 26, 2024

Closes: #6635

  • Add unit test for websocket controller
  • Add mock for websocket connection
  • Add mock for data provider
  • Add mock for data provider factory
  • Add mock for websocket connection

The WebSocket Controller interacts with:

  1. Data Provider: Supplies data to the controller.
  2. WebSocket Connection: Handles communication with the client.

To properly test the controller's logic, we mock these interactions. Since the controller runs two parallel routines (reader and writer), the tests also ensure both can shut down cleanly.
A done channel is used in the tests to coordinate this process.

* Add unit test for websocket controller
* Add mock for websocket connection
* Add mock for data provider
* Add mock for data provider factory
* Add mock for websocket connection

The WebSocket Controller interacts with:
1. Data Provider: Supplies data to the controller.
2. WebSocket Connection: Handles communication with the client.

To properly test the controller's logic, we mock these interactions.
Since the controller runs two parallel routines (reader and writer),
the tests also ensure both can shut down cleanly.
A done channel is used in the tests to coordinate this process.
@codecov-commenter
Copy link

codecov-commenter commented Nov 26, 2024

Codecov Report

Attention: Patch coverage is 61.63522% with 61 lines in your changes missing coverage. Please review.

Project coverage is 41.23%. Comparing base (a164070) to head (8496af3).
Report is 3 commits behind head on master.

Files with missing lines Patch % Lines
engine/access/rest/websockets/controller.go 70.00% 12 Missing and 6 partials ⚠️
engine/access/rest/websockets/connection.go 0.00% 12 Missing ⚠️
...ccess/rest/websockets/mock/websocket_connection.go 78.57% 4 Missing and 5 partials ⚠️
engine/access/rest/websockets/handler.go 0.00% 7 Missing ⚠️
...est/websockets/data_provider/mock/data_provider.go 58.33% 2 Missing and 3 partials ⚠️
...cess/rest/websockets/data_provider/mock/factory.go 73.68% 2 Missing and 3 partials ⚠️
engine/access/rest/router/router.go 0.00% 2 Missing ⚠️
...ine/access/rest/websockets/data_provider/blocks.go 0.00% 2 Missing ⚠️
...ne/access/rest/websockets/data_provider/factory.go 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #6762      +/-   ##
==========================================
+ Coverage   39.71%   41.23%   +1.51%     
==========================================
  Files        1332     2064     +732     
  Lines      123050   182805   +59755     
==========================================
+ Hits        48875    75383   +26508     
- Misses      69986   101096   +31110     
- Partials     4189     6326    +2137     
Flag Coverage Δ
unittests 41.23% <61.63%> (+1.51%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@illia-malachyn
Copy link
Contributor Author

illia-malachyn commented Nov 26, 2024

There are no tests for error cases. I think we should add them as part of #6642.
This PR is mostly like a skeleton for tests to enable other team members to write them

@Guitarheroua Guitarheroua changed the title Add unit test for websocket controller [Access] Add unit test for websocket controller Nov 26, 2024
Copy link
Contributor

@Guitarheroua Guitarheroua left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After first round of review - looks good!

) *Controller {
return &Controller{
logger: logger.With().Str("component", "websocket-controller").Logger(),
config: config,
conn: conn,
communicationChannel: make(chan interface{}), //TODO: should it be buffered chan?
communicationChannel: make(chan interface{}, 10), //TODO: should it be buffered chan?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my preference is to not buffer this channel. there are buffers in the dp subscriptions already so I don't think we need additional layers of buffers

engine/access/rest/websockets/controller.go Show resolved Hide resolved
}(c.conn)
c.shutdownOnce.Do(func() {
defer func() {
close(c.communicationChannel)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we should close this here. see discussion here: #6757 (comment)

func (c *Controller) writeMessagesToClient(ctx context.Context) {
//TODO: can it run forever? maybe we should cancel the ctx in the reader routine
func (c *Controller) writeMessages(ctx context.Context) {
defer c.shutdownConnection()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is deferred in both readMessages and writeMessages, which are both originally called in HandleConnection. you could avoid the synchronization you added in shutdownConnection by just calling it serially at the end of HandleConnection

defer c.shutdownConnection()

for {
select {
case <-ctx.Done():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this no longer needed? Is the client connection guaranteed to exit?

Topic: dp.Topic(),
ID: dp.ID().String(),
}
c.communicationChannel <- response
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should use a select to abort the push and return if the context is canceled.

Comment on lines +93 to +97
_, ok := <-done
if !ok {
return websocket.ErrCloseSent
}
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this return nil? if not, consider refactoring to make it clear for the next person to modify these tests

Suggested change
_, ok := <-done
if !ok {
return websocket.ErrCloseSent
}
return nil
<-done
return websocket.ErrCloseSent

require.True(t, ok)
msg, err := json.Marshal(requestMessage)
require.NoError(t, err)
*reqMsg = msg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what does this do?

if !ok {
return websocket.ErrCloseSent
}
return nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same comment about simplifying

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Access] Implement web socket connection mock for unit testing of web socket controller
5 participants